package com.hyy.dao.daoImpl;

import com.hyy.bean.MaskUser;
import com.hyy.dao.UserDao;
import com.hyy.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {


	public int deleteByPrimaryKey(Integer ID) {
		return 0;
	}

	public int insert(MaskUser user) throws SQLException{

		Connection conn = JdbcUtil.getConnection();
		//获取语句对象
		String sql = "insert into mask_user(useraccount, username, password, isadmin) value (?,?," +
				"?,?)";

		//设置参数
		PreparedStatement pstmt  = conn.prepareStatement(sql);
		//设置参数
		pstmt.setString(1,user.getUseraccount());
		pstmt.setString(2,user.getUsername());
		pstmt.setString(3,user.getPassword());
		pstmt.setLong(4,user.getIsadmin());
		//执行
		int num =  pstmt.executeUpdate();
		//关闭资源
		JdbcUtil.release(conn,pstmt);
		return num;
	}

	public MaskUser selectByPrimaryKey(Integer ID) {
		return null;
	}

	public List<MaskUser> selectAll() {
		return null;
	}

	public boolean updateByPrimaryKey(MaskUser user) throws SQLException{
		Connection conn = JdbcUtil.getConnection();

		String sql = "update mask_user set useraccount=?,password = ? where userid = ?";
		PreparedStatement pstmt  = conn.prepareStatement(sql);
		pstmt.setString(1,user.getUseraccount());
		pstmt.setString(2,user.getPassword());
		pstmt.setLong(3,user.getUserid());

		int n = pstmt.executeUpdate();

		return n==1?true:false;
	}

	public boolean checkUsername(String username) throws SQLException{

		Connection conn = JdbcUtil.getConnection();
		//获取语句对象
		String sql = "select userid from mask_user where username = ?";
		try {
			//设置参数
			PreparedStatement pstmt  = conn.prepareStatement(sql);
			pstmt.setString(1,username);

			//执行
			ResultSet rs =  pstmt.executeQuery();
			while(rs.next()){
				return false;
			}
			//关闭资源
			JdbcUtil.release(conn,pstmt,rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}

	public MaskUser selectLogin(String username, String password) throws SQLException{

		MaskUser user = null;
		Connection conn = JdbcUtil.getConnection();
		//获取语句对象
		String sql = "select userid,useraccount,username,password,isadmin from mask_user where username" +
				" = ? and " +
				"password " +
				"= ? and " +
				"isadmin = 0";

		//设置参数
		PreparedStatement pstmt  = conn.prepareStatement(sql);
		pstmt.setString(1,username);
		pstmt.setString(2,password);
			//执行
		ResultSet rs =  pstmt.executeQuery();
		while(rs.next()){
			user = new MaskUser(rs.getLong(1),rs.getString(2),rs.getString(3),rs.getString(4),
					rs.getLong(5));
		}//关闭资源
		JdbcUtil.release(conn,pstmt,rs);
		return user;
	}

	public MaskUser selectAdminLogin(String username, String password) {
		return null;
	}

	public boolean updatePasswordByUserId(Long userId, String newPassword) {
		MaskUser user = null;
		Connection conn = JdbcUtil.getConnection();
		//获取语句对象
		String sql = "update mask_user set password = ? where userid = ?";
		try {
			//设置参数
			PreparedStatement pstmt  = conn.prepareStatement(sql);
			pstmt.setString(1,newPassword);
			pstmt.setLong(2,userId);
			//执行
			int num = pstmt.executeUpdate();
			//关闭资源
			JdbcUtil.release(conn,pstmt);
			return num==1?true:false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}
